import { ViewNode } from '../viewmodel/viewNode'

import nativeQCardSdk from 'libQuickCardSdk.so'; // QuickCard

import convertxml from '@ohos.convertxml';
import fileio from '@ohos.fileio';

import { dsl_case1, dsl_update_event } from '../test/case_1'
import { dsl_case2 } from '../test/case_2'
import { dsl_case3 } from '../test/case_3'
import { dsl_case_scroller } from '../test/case_scroller'
import { dsl_case_foreach } from '../test/case_foreach'

export const UpdateAndScanXml = (url: string): ViewNode =>  {
    return dsl_update_event;
}
export const LoadAndScanXml = (url: string): ViewNode =>  {
    // console.error("Kee zx LoadAndScanXml url = " + url)
//    var dsl = null;
//    try {
//        dsl = nativeQCardSdk.qCardRender("qCardRender");
//        // console.error("Kee zx nativeQCardSdk: dsl = " + JSON.stringify(dsl));
//    } catch (e) {
//        // console.error("Kee zx nativeQCardSdk Parse DSL Error")
//    }
//    return dsl;

    // File绝对路径不能有`file://`
    var fileCompletePath = "/data/storage/el1/bundle/com.example.entry/assets/js/MainAbility/test/developer_mode.xml";
    var path = "/data/app/el2/100/base/com.example.dynamic/files/test.json";
    let result;
    let xmlResult : any;
    let dsl : string = fileio.readTextSync(fileCompletePath);
    // console.error('Kee zx xmlResult readTextSync 1 ')
        try {
            var xmlInstance = new convertxml.ConvertXML();
            dsl = dsl.replace(/\r\n/g, "");
             xmlResult = xmlInstance.convert(dsl, {
                trim: false,
//                ignoreAttributes: true,
//                ignoreComment: true,
                declarationKey: 'declaration',
                instructionKey: 'instruction',
                attributesKey: 'attributes',
                textKey: 'text',
                cdataKey: 'cdata',
                doctypeKey: 'doctype',
                commentKey: 'comment',
                parentKey: 'parent',
                typeKey: 'type',
                nameKey: 'viewName',
                elementsKey: 'children'
            });
            if (xmlResult) {
//                result =  JSON.stringify(xmlResult)
//                 console.error('Kee zx xmlResult result = ',typeof  xmlResult)
//                 console.error('Kee zx xmlResult result = ', result)
            } else {
                // console.error('Kee zx xmlResult error ')
            }
        }catch (e) {
             // console.error('Kee zx xmlResult try checker failed 11 ', e)
        }


//    return xmlResult;
//    return dsl_case1;
//    return dsl_case2;
//    return dsl_case_scroller;
//    return dsl_case_foreach;
//    // console.error('Kee zx return xmlResult = ', JSON.stringify(xmlResult))
    return xmlResult;
}